﻿using System.Collections;

namespace Devonline.AspNetCore;

/// <summary>
/// 适用于显式的分页参数查询的返回值
/// </summary>
public class PagedResult : IEnumerable
{
    public PagedResult()
    {
        PageIndex = AppSettings.UNIT_ONE;
        PageSize = AppSettings.UNIT_TEN;
    }

    /// <summary>
    /// 查询结果总数目
    /// </summary>
    public int Total { get; set; }
    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex { get; set; }
    /// <summary>
    /// 页大小
    /// </summary>
    public int PageSize { get; set; }
    /// <summary>
    /// 当前页数据集合
    /// </summary>
    public virtual IEnumerable? Rows { get; set; }

    /// <summary>
    /// 对内部 Rows 实现迭代器
    /// </summary>
    /// <returns></returns>
    public virtual IEnumerator GetEnumerator()
    {
        if (Rows == null)
        {
            throw new ArgumentNullException(nameof(Rows), "Rows can't be null!");
        }

        return Rows.GetEnumerator();
    }
}

/// <summary>
/// 适用于显式的分页参数查询的返回值
/// </summary>
public class PagedResult<T> : IEnumerable<T>
{
    public PagedResult()
    {
        PageIndex = AppSettings.UNIT_ONE;
        PageSize = AppSettings.UNIT_TEN;
    }

    /// <summary>
    /// 查询结果总数目
    /// </summary>
    public int Total { get; set; }
    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex { get; set; }
    /// <summary>
    /// 页大小
    /// </summary>
    public int PageSize { get; set; }
    /// <summary>
    /// 当前页数据集合
    /// </summary>
    public virtual IEnumerable<T>? Rows { get; set; }

    /// <summary>
    /// 对内部 Rows 实现迭代器
    /// </summary>
    /// <returns></returns>
    public virtual IEnumerator<T> GetEnumerator()
    {
        if (Rows == null)
        {
            throw new ArgumentNullException(nameof(Rows), "Rows can't be null!");
        }

        return Rows.GetEnumerator();
    }
    /// <summary>
    /// 对内部 Rows 实现迭代器
    /// </summary>
    /// <returns></returns>
    IEnumerator IEnumerable.GetEnumerator()
    {
        if (Rows == null)
        {
            throw new ArgumentNullException(nameof(Rows), "Rows can't be null!");
        }

        return Rows.GetEnumerator();
    }
}